// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期：2024-11-11 17:42:22
// 生成路径: internal/app/system/controller/schools.go
// 生成人：王中阳
// desc:学校
// company:云南奇讯科技有限公司
// ==========================================================================

package controller

import (
	"context"

	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/tiger1103/gfast/v3/api/v1/system"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/xuri/excelize/v2"
)

type schoolsController struct {
	BaseController
}

var Schools = new(schoolsController)

// List 列表
func (c *schoolsController) List(ctx context.Context, req *system.SchoolsSearchReq) (res *system.SchoolsSearchRes, err error) {
	res = new(system.SchoolsSearchRes)
	res.SchoolsSearchRes, err = service.Schools().List(ctx, &req.SchoolsSearchReq)
	return
}

// Export 导出excel
func (c *schoolsController) Export(ctx context.Context, req *system.SchoolsExportReq) (res *system.SchoolsExportRes, err error) {
	var (
		r        = ghttp.RequestFromCtx(ctx)
		listData []*model.SchoolsInfoRes
		//表头
		tableHead = []interface{}{"", "省份", "城市", "所属部门", "等级", "学校名称", "类型", "编码", "是否显示 1是 0否", "经度", "纬度", ""}
		excelData [][]interface{}
		//字典选项处理
	)
	req.PageNum = 1
	req.PageSize = 500
	//获取字典数据
	excelData = append(excelData, tableHead)
	for {
		listData, err = service.Schools().GetExportData(ctx, &req.SchoolsSearchReq)
		if err != nil {
			return
		}
		if listData == nil {
			break
		}
		for _, v := range listData {
			var ()
			dt := []interface{}{
				v.Id,
				v.Province,
				v.City,
				v.Department,
				v.Level,
				v.Schoolname,
				v.Type,
				v.Code,
				v.Show,
				v.Lat,
				v.Lng,
				v.CreatedAt.Format("Y-m-d H:i:s"),
			}
			excelData = append(excelData, dt)
		}
		req.PageNum++
	}
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("学校信息表")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *schoolsController) ExcelTemplate(ctx context.Context, req *system.SchoolsExcelTemplateReq) (res *system.SchoolsExcelTemplateRes, err error) {
	var (
		r = ghttp.RequestFromCtx(ctx)
		//表头
		tableHead = []interface{}{"省份", "城市", "所属部门", "等级", "学校名称", "类型", "编码", "是否显示 1是 0否", "经度", "纬度", ""}
		excelData = [][]interface{}{tableHead}
	)
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("学校信息表模板")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *schoolsController) Import(ctx context.Context, req *system.SchoolsImportReq) (res *system.SchoolsImportRes, err error) {
	err = service.Schools().Import(ctx, req.File)
	return
}

// Get 获取学校
func (c *schoolsController) Get(ctx context.Context, req *system.SchoolsGetReq) (res *system.SchoolsGetRes, err error) {
	res = new(system.SchoolsGetRes)
	res.SchoolsInfoRes, err = service.Schools().GetById(ctx, req.Id)
	return
}

// Add 添加学校
func (c *schoolsController) Add(ctx context.Context, req *system.SchoolsAddReq) (res *system.SchoolsAddRes, err error) {
	err = service.Schools().Add(ctx, req.SchoolsAddReq)
	return
}

// Edit 修改学校
func (c *schoolsController) Edit(ctx context.Context, req *system.SchoolsEditReq) (res *system.SchoolsEditRes, err error) {
	err = service.Schools().Edit(ctx, req.SchoolsEditReq)
	return
}

// Delete 删除学校
func (c *schoolsController) Delete(ctx context.Context, req *system.SchoolsDeleteReq) (res *system.SchoolsDeleteRes, err error) {
	err = service.Schools().Delete(ctx, req.Ids)
	return
}
